import cx_Oracle as cxo
import datetime as time

# 指定数据文件，并将数据导入到指定的表中
# fname     date file
# tname     table name
# 
def loaddata(fname, tname):
    conn = cxo.connect('scott/tiger@192.168.0.137/ORCL')
    c = conn.cursor()
    rs = c.execute("select column_name, datatype, data_precision, data_scale from user_tab_columns where table_name = '" + tname + "'")
    meta = rs.fetchall()
    o_data = []
    t_data = []
    
    with open(fname) as df:
        for line in df:
            o_data.append(line.strip().replace('"', '').split(','))

    for r_d in o_data:
        for i in range(len(meta)):
            if (meta[i][1] == 'VARCHAR2'):
                continue
            if r_d[i] == '':
                r_d[i] = None
            else:
                if (meta[i][1] == 'DATE'):
                    r_d[i] = time.datetime.striptime(r_d[i], '%Y/$m/$d')
                if (meta[i][1] == 'NUMBER'):
                    if (meta[i][3] == 0):
                        r_d[i] = int(r_d[i])
                    else:
                        r_d[i] = float(r_d[i])
        
    c.executemany(sql, t_data)
    conn.commit()
        
    c.close()
    conn.close()
path = 'D:\18.pycharm\1.text\work\emp.csv'
tname = 'EMP'
loaddata(path, tname)
